﻿using System;
using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class Reamerheadindexableinserts
    {
        public static void _Reamerheadindexableinserts(Dictionary<string, Object> data)//细柄丝锥
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            refin.Linetype = Linetype.Dashed;
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            centre.Linetype = Linetype.DashDot;
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            double scale = 2; //放大系数


            double oal = 29.73;
            double dconms = 6.65 * 2;
            double dc = 22.32;
            double dhub = 19.52;
            double plgang = 45;
            double l = 22.38;


            //坐标
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X - oal - 4, v1.Y);
            Vector2 v3 = new Vector2(v1.X - 2, v1.Y);
            Vector2 v4 = new Vector2(v3.X, v3.Y + dhub / 2);
            Vector2 v5 = new Vector2(v4.X - (dc - dhub) / 2 / Math.Tan(plgang * pi / 180), v3.Y + dc / 2);
            Vector2 v6 = new Vector2(v4.X - l, v5.Y);
            Vector2 v7 = new Vector2(v4.X - l / 0.945, v4.Y);
            Vector2 v8 = new Vector2(v4.X - oal, v4.Y);
            Vector2 v9 = new Vector2(v8.X, v3.Y + dhub / 7.808);
            Vector2 v10 = new Vector2(v9.X + oal / 30.969, v3.Y + dhub / 13.189);
            Vector2 v11 = new Vector2(v9.X + oal / 12.185, v10.Y);
            Vector2 v12 = new Vector2(v9.X + oal / 9.092, v3.Y + dhub / 28.29);
            Vector2 v13 = new Vector2(v12.X, v12.Y * -1);
            Vector2 v14 = new Vector2(v11.X, v11.Y * -1);
            Vector2 v15 = new Vector2(v10.X, v10.Y * -1);
            Vector2 v16 = new Vector2(v9.X, v9.Y * -1);
            Vector2 v17 = new Vector2(v8.X, v8.Y * -1);
            Vector2 v18 = new Vector2(v3.X - l / 0.969, v17.Y);
            Vector2 v19 = new Vector2(v18.X + l / 54.585, v3.Y - dc / 2.167);
            Vector2 v20 = new Vector2(v5.X, v19.Y);
            Vector2 v21 = new Vector2(v3.X, v3.Y - dc / 2.604);
            Vector2 v22 = new Vector2(v4.X, v4.Y * -1);
            Vector2 v23 = new Vector2(v5.X, v5.Y * -1);
            Vector2 v24 = new Vector2(v6.X, v6.Y * -1);
            Vector2 v25 = new Vector2(v7.X, v7.Y * -1);
            Vector2 v26 = new Vector2(v4.X - l / 2.28, v4.Y);
            Vector2 v27 = new Vector2(v26.X, v26.Y + (v5.Y - v4.Y) / 2);
            Vector2 v28 = new Vector2(v27.X - l / 5.723, v27.Y);
            Vector2 v29 = new Vector2(v28.X, v26.Y);
            Vector2 v30 = new Vector2(v29.X + l / 26.891, v29.Y - dhub / 22.437);
            Vector2 v31 = new Vector2(v26.X - l / 26.891, v30.Y);
            Vector2 v32 = new Vector2(v4.X, v4.Y - dhub / 12.049);
            Vector2 v33 = new Vector2(v7.X + 0.5, v32.Y);
            Vector2 v34 = new Vector2(v7.X, v33.Y + 0.5);
            Vector2 v35 = new Vector2(v3.X - oal / 3.087, v3.Y);
            Vector2 v36 = new Vector2(v35.X, v35.Y + dhub / 3.881);
            Vector2 v37 = new Vector2(v35.X - oal / 36.256, v35.Y + dhub / 3.303);
            Vector2 v38 = new Vector2(v35.X - oal / 4.609, v35.Y + dhub / 3.012);
            Vector2 v39 = new Vector2(v35.X - oal / 4.007, v35.Y + dhub / 3.498);
            Vector2 v40 = new Vector2(v39.X, v35.Y);
            Vector2 v41 = new Vector2(v9.X + oal / 7.012, v3.Y);
            Vector2 v42 = new Vector2(v41.X, v41.Y + dhub / 2.935);
            Vector2 v43 = new Vector2(v8.X, v42.Y);
            Vector2 v44 = new Vector2(v41.X, v41.Y + dhub / 3.221);
            Vector2 v45 = new Vector2(v40.X, v40.Y + dhub / 3.725);
            Vector2 v46 = new Vector2(v35.X, v35.Y + dhub / 4.318);
            Vector2 v47 = new Vector2(v3.X, v3.Y + dhub / 5.425);
            Vector2 v48 = new Vector2(v4.X + oal / 10.733, v4.Y + dc / 4.729);
            Vector2 v49 = new Vector2(v48.X - oal / 3.626, v48.Y);
            Vector2 v50 = new Vector2(v49.X, v49.Y - dc / 2.673);
            Vector2 v51 = new Vector2(v48.X, v50.Y);
            Vector2 v66 = new Vector2(v43.X - dconms / 2, v43.Y);

            Vector2 v52 = new Vector2(v1.X - oal / 2.095, v1.Y - dc / 0.582);
            Vector2 v53 = new Vector2(v52.X - (dc - dhub) / 2 / 0.729 / Math.Tan(plgang * pi / 180), v52.Y + (dc - dhub) / 2 / 0.729);
            Vector2 v54 = new Vector2(v52.X - l / 3.045, v53.Y);
            Vector2 v55 = new Vector2(v52.X, v52.Y - dc / 4.518);
            Vector2 v56 = new Vector2(v52.X, v52.Y - dhub / 8.414);
            Vector2 v57 = new Vector2(v54.X, v56.Y);
            Vector2 v58 = new Vector2(v57.X, v55.Y);
            Vector2 v59 = new Vector2(v54.X, v52.Y);
            Vector2 v60 = new Vector2(v52.X - l / 1.776, v52.Y + dc / 2.906);
            Vector2 v61 = new Vector2(v60.X + l / 0.925, v60.Y);
            Vector2 v62 = new Vector2(v61.X, v61.Y - dc / 1.464);
            Vector2 v63 = new Vector2(v60.X, v62.Y);
            Vector2 v64 = new Vector2(v52.X, v53.Y);
            Vector2 v65 = new Vector2(v64.X + l / 5.738, v64.Y - dc / 10.835);




            //作图
            Line line1 = new Line(v1, v2);
            line1.Layer = centre;
            line1.Linetype = Linetype.DashDot;
            line1.LinetypeScale = 3;
            dxf.AddEntity(line1);

            LwPolyline line2 = new LwPolyline();
            line2.Vertexes.Add(new LwPolylineVertex(v21));
            line2.Vertexes.Add(new LwPolylineVertex(v20));
            line2.Vertexes.Add(new LwPolylineVertex(v19));
            line2.Vertexes.Add(new LwPolylineVertex(v18));
            line2.Vertexes.Add(new LwPolylineVertex(v17));
            line2.Vertexes.Add(new LwPolylineVertex(v16));
            line2.Vertexes.Add(new LwPolylineVertex(v15));
            line2.Vertexes.Add(new LwPolylineVertex(v14));
            line2.Vertexes.Add(new LwPolylineVertex(v13));
            line2.Vertexes.Add(new LwPolylineVertex(v12));
            line2.Vertexes.Add(new LwPolylineVertex(v11));
            line2.Vertexes.Add(new LwPolylineVertex(v10));
            line2.Vertexes.Add(new LwPolylineVertex(v9));
            line2.Vertexes.Add(new LwPolylineVertex(v8));
            line2.Vertexes.Add(new LwPolylineVertex(v7));
            line2.Vertexes.Add(new LwPolylineVertex(v6));
            line2.Vertexes.Add(new LwPolylineVertex(v5));
            line2.Vertexes.Add(new LwPolylineVertex(v4));
            line2.Vertexes.Add(new LwPolylineVertex(v22));
            line2.Vertexes.Add(new LwPolylineVertex(v23));
            line2.Vertexes.Add(new LwPolylineVertex(v24));
            line2.Vertexes.Add(new LwPolylineVertex(v25));
            fun.PolyFill(dxf, line2, 0.2, contour);

            LwPolyline line3 = new LwPolyline();
            line3.Vertexes.Add(new LwPolylineVertex(v26));
            line3.Vertexes.Add(new LwPolylineVertex(v31));
            line3.Vertexes.Add(new LwPolylineVertex(v30));
            line3.Vertexes.Add(new LwPolylineVertex(v29));
            line3.Vertexes.Add(new LwPolylineVertex(v28));
            line3.Vertexes.Add(new LwPolylineVertex(v27));
            line3.Vertexes.Add(new LwPolylineVertex(v26));
            line3.Vertexes.Add(new LwPolylineVertex(v4));
            line3.Vertexes.Add(new LwPolylineVertex(v32));
            line3.Vertexes.Add(new LwPolylineVertex(v33));
            line3.Vertexes.Add(new LwPolylineVertex(v34));
            line3.Vertexes.Add(new LwPolylineVertex(v7));
            line3.Vertexes.Add(new LwPolylineVertex(v29));
            line3.Vertexes[0].Bulge = Math.Tan(-91 * pi / 180 / 4);
            line3.Vertexes[2].Bulge = Math.Tan(-91 * pi / 180 / 4);
            line3.Vertexes[9].Bulge = Math.Tan(-90 * pi / 180 / 4);
            fun.PolyFill(dxf, line3, 0.2, contour);

            LwPolyline line4 = new LwPolyline();
            line4.Vertexes.Add(new LwPolylineVertex(v47));
            line4.Vertexes.Add(new LwPolylineVertex(v46));
            line4.Vertexes.Add(new LwPolylineVertex(v35));
            line4.Vertexes.Add(new LwPolylineVertex(v36));
            line4.Vertexes.Add(new LwPolylineVertex(v37));
            line4.Vertexes.Add(new LwPolylineVertex(v38));
            line4.Vertexes.Add(new LwPolylineVertex(v39));
            line4.Vertexes.Add(new LwPolylineVertex(v40));
            line4.Vertexes.Add(new LwPolylineVertex(v45));
            line4.Vertexes.Add(new LwPolylineVertex(v44));
            line4.Vertexes.Add(new LwPolylineVertex(v41));
            line4.Vertexes.Add(new LwPolylineVertex(v42));
            line4.Vertexes.Add(new LwPolylineVertex(v43));
            line4.Vertexes[3].Bulge = Math.Tan(87 * pi / 180 / 4);
            line4.Vertexes[5].Bulge = Math.Tan(97 * pi / 180 / 4);
            fun.PolyFill(dxf, line4, 0.2, contour);

            Line line5 = new Line(v48, v49);
            line5.Layer = refin;
            line5.Linetype = Linetype.Dashed;
            line5.LinetypeScale = 5;
            dxf.AddEntity(line5);

            Line line6 = new Line(v48, v51);
            line6.Layer = refin;
            line6.Linetype = Linetype.Dashed;
            line6.LinetypeScale = 5;
            dxf.AddEntity(line6);

            Line line7 = new Line(v50, v49);
            line7.Layer = refin;
            line7.Linetype = Linetype.Dashed;
            line7.LinetypeScale = 5;
            dxf.AddEntity(line7);

            Line line8 = new Line(v50, v51);
            line8.Layer = refin;
            line8.Linetype = Linetype.Dashed;
            line8.LinetypeScale = 5;
            dxf.AddEntity(line8);

            LwPolyline line15 = new LwPolyline();
            line15.Vertexes.Add(new LwPolylineVertex(v66));
            line15.Vertexes.Add(new LwPolylineVertex(v43));
            fun.PolyFill(dxf, line15, 0.05, dimensioning);


            //2
            Line line9 = new Line(v60, v61);
            line9.Layer = refin;
            line9.Linetype = Linetype.Dashed;
            line9.LinetypeScale = 5;
            dxf.AddEntity(line9);

            Line line10 = new Line(v60, v63);
            line10.Layer = refin;
            line10.Linetype = Linetype.Dashed;
            line10.LinetypeScale = 5;
            dxf.AddEntity(line10);

            Line line11 = new Line(v61, v62);
            line11.Layer = refin;
            line11.Linetype = Linetype.Dashed;
            line11.LinetypeScale = 5;
            dxf.AddEntity(line11);

            Line line12 = new Line(v62, v63);
            line12.Layer = refin;
            line12.Linetype = Linetype.Dashed;
            line12.LinetypeScale = 5;
            dxf.AddEntity(line12);

            LwPolyline line13 = new LwPolyline();
            line13.Vertexes.Add(new LwPolylineVertex(v57));
            line13.Vertexes.Add(new LwPolylineVertex(v56));
            line13.Vertexes.Add(new LwPolylineVertex(v55));
            line13.Vertexes.Add(new LwPolylineVertex(v52));
            line13.Vertexes.Add(new LwPolylineVertex(v59));
            line13.Vertexes.Add(new LwPolylineVertex(v52));
            line13.Vertexes.Add(new LwPolylineVertex(v53));
            line13.Vertexes.Add(new LwPolylineVertex(v54));
            fun.PolyFill(dxf, line13, 0.2, contour);

            LwPolyline line14 = new LwPolyline();
            line14.Vertexes.Add(new LwPolylineVertex(v64));
            line14.Vertexes.Add(new LwPolylineVertex(v65));
            fun.PolyFill(dxf, line14, 0.05, dimensioning);


            fun.drawcircle(dxf, v5, 1, 0.01, dimensioning);




            //填充
            LwPolyline hatchpoly1 = new LwPolyline();
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v36));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v37));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v38));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v39));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v45));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v44));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v42));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v43));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v8));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v7));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v34));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v33));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v32));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v47));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v46));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v36));
            hatchpoly1.Vertexes[0].Bulge = Math.Tan(87 * pi / 180 / 4);
            hatchpoly1.Vertexes[2].Bulge = Math.Tan(97 * pi / 180 / 4);
            hatchpoly1.Vertexes[10].Bulge = Math.Tan(90 * pi / 180 / 4);
            hatchpoly1.IsClosed = true;
            String[] htype1 = new String[100];
            Line[] hline1 = new Line[100];
            Arc[] harc1 = new Arc[100];
            int hnumst1 = hatchpoly1.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst1; i++)
            {
                htype1[i] = hatchpoly1.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype1[i] == "Line")
                {
                    hline1[i] = (Line)hatchpoly1.Explode()[i];
                }
                if (htype1[i] == "Arc")
                {
                    harc1[i] = (Arc)hatchpoly1.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype1, hnumst1, 45, 10, hline1, harc1, hatching);

            LwPolyline hatchpoly2 = new LwPolyline();
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v55));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v58));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v57));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v56));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v55));
            hatchpoly2.IsClosed = true;
            String[] htype2 = new String[100];
            Line[] hline2 = new Line[100];
            Arc[] harc2 = new Arc[100];
            int hnumst2 = hatchpoly2.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst2; i++)
            {
                htype2[i] = hatchpoly2.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype2[i] == "Line")
                {
                    hline2[i] = (Line)hatchpoly2.Explode()[i];
                }
                if (htype2[i] == "Arc")
                {
                    harc2[i] = (Arc)hatchpoly2.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype2, hnumst2, 45, 10, hline2, harc2, hatching);



            //坐标系
            fun.Axis(dxf, new Vector2(v9.X, v3.Y), -90, 180, "", "", "PCS,MCS", 0.5, 2, 0);

            fun.Axis(dxf, v3, -90, 180, "", "", "CIP", 0.5, 2, 0);

            fun.Axis(dxf, new Vector2(v52.X, v53.Y), -90, 180, "X", "Y", "Z", 0.5, 2, 0);



            //标注

            double height = 2;
            double arrowsize = 2;
            double exline = 2;



            fun.dimrot1(dxf, v17, v22, 180, dc / 2, 1, height, arrowsize, exline, "OAL", "", "");

            fun.dimrot1(dxf, v24, v22, 180, dc / 4, 1, height, arrowsize, exline, "L", "", "");

            fun.dimrot1(dxf, v5, v4, 0, dc / 3, 1, height, arrowsize, exline, "PLGL", "", "");

            fun.dimrot1(dxf, v5, v8, 0, dc / 3, 1, height, arrowsize, exline, "LF,LTA", "", "");

            fun.dimrot1(dxf, v5, v23, -90, dc / 2, 1, height, arrowsize, exline, "DC", "", "");

            fun.dimrot1(dxf, v8, v17, 90, dconms, 1, height, arrowsize, exline, "DCONMS", "", "");

            fun.jdbz2(dxf, new Line(v5, fun.Polar(v5, (180-plgang) * pi / 180, 0.1)), new Line(v5, fun.Polar(v5, 180 * pi / 180, 0.1)), l/3, height, arrowsize, exline, "PLGANF", "", "");

            fun.text1(dxf, new Vector2(v5.X + oal / 13.155, v5.Y + dc / 72), 90, "CRP");

            fun.text1(dxf, new Vector2(v65.X + l / 24.326, v65.Y - dc / 15.5), 90, "CSW");

            fun.jt(dxf, new Vector2(v1.X + oal / 2.532, v1.Y - dc / 1.344), 180, 9, height, "FDP");

            fun.jt(dxf, new Vector2(v66.X + 1.5, v66.Y), -90, 9, height, "DCONMS");

            fun.jt(dxf, new Vector2(v66.X + 3, v66.Y), 135, 9, height, "CCMS");




            //定义格式，路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);


        }

    }
}